<?php
include_once('common/header.php');	
if($AppUI->id <= 0 || !isset($_SERVER['HTTP_REFERER'])) {	
	$arrJson = array('error'=>1,'msg'=>$arrError[1]);
	echo $objRequest->getParam('callback') . '(' . Zend_Json::encode($arrJson) . ')';
	exit();
}
$intError		= 0;
$strMessage		= '';
$strData		= '';
$strSecret		= '';
$strProductName	= '';
$intQuantity 	= 0;
$intPrice 		= 0;
$strRefer		= isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$intFgiftId		= $objRequest->getParam('fgiftid',0);
$intTargetId	= Vfriend_Function::base64D($objRequest->getParam('uid',''));
$strToken		= $objRequest->getParam('token','');
$strSession		= Vfriend_Function::getSecret($intFgiftId, SE_FULLHOUSE_GIFT_ACCEPT);
if($intFgiftId && $intTargetId && $strToken != '' && $strSession != '' && $strSession == $strToken && Vfriend_Function::isValidLink($strRefer)) {
	Vfriend_Function::deleteSecret($intFgiftId, SE_FULLHOUSE_GIFT_ACCEPT);	
	$objVfriendUser	= new Vfriend_User();
	$objURequest	= new URequest();
	$objTarget		= $objVfriendUser->getInfo($intTargetId);	
	$arrResult		= $objURequest->acceptGift($AppUI->id, $intTargetId, $intFgiftId, $intError);
	$strProductName	= isset($arrResult['name']) ? $arrResult['name'] : '';
	$intQuantity 	= isset($arrResult['quantity']) ? $arrResult['quantity'] : 0;
	$intPrice 		= isset($arrResult['price']) ? $arrResult['price'] : 0;
	if($intError == 0) {
		$objURequest->deleteRequestByRefObjId($intFgiftId,$AppUI->id, $intTargetId,1);
		$strMessage	= $arrMessage[27];
		$strMessage	= str_replace('[strFullname]', $objTarget->fullnamevn, $strMessage);		
	}else {
		if($intError == 2) $strSecret = Vfriend_Function::setSecret($intFgiftId, SE_FULLHOUSE_GIFT_SELL);
		$intError += 34;
	}
	if($intError == 0 || $intError == 36) {
		$intUserProId = $AppUI->id;
		include_once('fullhouse/box-gift.php');	
		$objView 	= Zend_Registry::get('objView');
		$strData	= $objView->fetch('fullhouse/box-gift.tpl');
	}
}else {
	$intError	= 27;
}
if($intError > 0) {
	$strMessage	= $arrError[$intError];	
}
$strMessage	= str_replace('[intQuantity]', Vfriend_Function::numberFormat($intQuantity), $strMessage);
$strMessage	= str_replace('[strProductName]', $strProductName, $strMessage);
$strMessage	= str_replace('[intPrice]', Vfriend_Function::numberFormat($intPrice), $strMessage);
$arrJson = array('error'=>$intError,'msg'=>$strMessage,'data'=>$strData,'secret'=>$strSecret);
echo $objRequest->getParam('callback') . '(' . Zend_Json::encode($arrJson) . ')';
exit();
?>